package 作业;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.IOException;
import java.io.Serializable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

public class getuser extends JFrame implements Serializable{

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField username;
	private JPasswordField password;
	private userinformation user=null;
	public static void main(String[] args) {
			getuser frame =new getuser();
			frame.setVisible(true);
	}

	public getuser() {
		try {
			user = new userinformation();
		} catch (ClassNotFoundException | IOException e1) {
			e1.printStackTrace();

		}
		setTitle("用户登陆");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 648, 451);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		
		username = new JTextField();
		username.setFont(new Font("宋体", Font.PLAIN, 22));
		username.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				password.requestFocus();
			}
		});
		username.setColumns(10);
		
		password = new JPasswordField();
		password.setFont(new Font("宋体", Font.PLAIN, 20));
		password.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
			}
		});
		
		JLabel lblNewLabel = new JLabel("用户名");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 21));
		
		JLabel lblNewLabel_1 = new JLabel("密 码");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 21));
		
		JLabel title = new JLabel("用户登陆");
		title.setFont(new Font("宋体", Font.PLAIN, 40));
		
		JButton enter = new JButton("登陆");
		enter.setFont(new Font("宋体", Font.PLAIN, 21));
		
		JButton out = new JButton("注册");
		out.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(password.getPassword().length<6||username.getText().trim().length()<1) {
					JOptionPane.showMessageDialog(password , "用户名不得为空且密码长度不得低于6位","提示",JOptionPane.ERROR_MESSAGE);
				}
				else {
					if (username.equals("admin")){
						JOptionPane.showMessageDialog(password , "禁止注册管理员","提示",JOptionPane.ERROR_MESSAGE);
					}
					else{
						try {
							if(user.newuser(username.getText(),new String(password.getPassword()))) {
								JOptionPane.showMessageDialog(password , "注册成功","提示",JOptionPane.OK_CANCEL_OPTION);
							}
							else {
								JOptionPane.showMessageDialog(password , "该用户已存在","提示",JOptionPane.ERROR_MESSAGE);
							}
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				}
			}
		});
		out.setFont(new Font("宋体", Font.PLAIN, 20));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(192)
					.addComponent(title, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(236, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(118, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(64)
							.addComponent(out, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(enter, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
							.addGap(147))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE))
							.addGap(31)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(password, GroupLayout.PREFERRED_SIZE, 271, GroupLayout.PREFERRED_SIZE)
								.addComponent(username, GroupLayout.PREFERRED_SIZE, 268, GroupLayout.PREFERRED_SIZE))
							.addGap(137))))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(28)
							.addComponent(title, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(username, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)))
					.addGap(56)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(password, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
					.addGap(51)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(enter, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
						.addComponent(out, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
					.addContainerGap(62, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
		password.addActionListener(new enterevent());
		enter.addActionListener(new enterevent());
	}
	class enterevent implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(username.getText().trim().length()<1||password.getPassword().length<6) {
				JOptionPane.showMessageDialog(password , "账户或密码错误，用户名不得为空且密码长度不得低于六位","提示",JOptionPane.ERROR_MESSAGE);
			}
			else {
				try {
//					if(user.check(username.getText(),new String(password.getPassword()))) {
//						Getdb.username=username.getText();
//						JOptionPane.showMessageDialog(password , "登陆成功","提示",JOptionPane.WARNING_MESSAGE);
//						dispose();
//						(new Maingui(user)).setVisible(true);}
					String type="";
					type=user.gettype(username.getText(),new String(password.getPassword()));
					if(type.length()>0){
						dispose();
						if(type.equals("U")){
							//用户界面
							Getdb.username=username.getText();
							JOptionPane.showMessageDialog(password , "用户登陆成功","提示",JOptionPane.WARNING_MESSAGE);
							(new Maingui(user)).setVisible(true);
						}
						else{
							//管理员界面
							JOptionPane.showMessageDialog(password , "管理员登陆成功","提示",JOptionPane.WARNING_MESSAGE);
							new AdJpanel();
						}
					}
					else {
						JOptionPane.showMessageDialog(password , "密码错误","提示",JOptionPane.ERROR_MESSAGE);
					}
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				} catch (Exception ex) {
					throw new RuntimeException(ex);
				}
			}
		}
		
	}
}


